package com.yellowpage.omc.ability.service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.ca.data.User;
import com.yellowpage.nsp.dao.RemoteNspDAO;
import com.yellowpage.omc.ability.dao.PartnerInterfaceDao;
import com.yellowpage.omc.ability.dao.PartnerParamValueDao;
import com.yellowpage.omc.ability.data.PartnerInterface;
import com.yellowpage.omc.ability.data.PartnerParamValue;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

public class PartnerInterfaceServiceImpl extends BaseServiceImpl implements
		PartnerInterfaceService {
	private PartnerInterfaceDao partnerInterfaceDao;
	private PartnerParamValueDao partnerParamValueDao;
	protected User loginUser;

	public void setPartnerInterfaceDao(PartnerInterfaceDao partnerInterfaceDao) {
		this.partnerInterfaceDao = partnerInterfaceDao;
	}

	/* Smooth generate Method */
	public PartnerInterface getPartnerInterface(Long id) {
		return partnerInterfaceDao.get(id);
	}

	public List<PartnerInterface> getAllPartnerInterface() {
		return partnerInterfaceDao.getAll();
	}

	public List<Map> queryPartnerInterfaceByPage(Map queryMap,
			PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"SELECT c.NAME as PARTNER_NAME, d.NAME as INTERFACE_NAME,"
								+ "       a.PARTNER_INTERFACE_ID , a.PARTNER_ID , a.INTERFACE_ID , "
								+ "       (select display_name from CA_USER where user_id = a.LAST_UPDATE_USER) as last_update_user, "
								+ "       (select display_name from CA_USER where user_id = a.CREATE_USER) as create_user, "
								+ "       a.CREATE_TIME , a.LAST_UPDATE_TIME , a.STATUS,a.LAST_SYNCHRONIZATION_TIME,(select display_name from CA_USER where user_id = a.LAST_SYNCHRONIZATION_USER) as LAST_SYNCHRONIZATION_USER    "
								+ "  FROM OMC_PARTNER_INTERFACE a, OMC_PARTNER c,  OMC_INTERFACE d "
								+ " WHERE a.PARTNER_ID = c.PARTNER_ID and a.INTERFACE_ID = d.INTERFACE_ID ")
				.addCondition(
						" and PARTNER_INTERFACE_ID = :partnerInterfaceId ")
				.addCondition(" and a.PARTNER_ID = :partnerId ")
				.addCondition(" and a.INTERFACE_ID = :interfaceId ")
				.addCondition(" and CREATE_USER = :createUser ")
				.addCondition(
						" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ")
				.addCondition(" and LAST_UPDATE_USER = :lastUpdateUser ")
				.addCondition(
						" and date_format(LAST_UPDATE_TIME,'%Y-%m-%d') =  :lastUpdateTime  ")
				.addCondition(" and a.STATUS = :status ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	//
	public void queryAbilityPartner(Long userId) {
		/*
		 * 1.获取nsp里面updatetime获取到 2。以最大的实际来对比数据 获取omc的数据
		 */

		RemoteNspDAO remoteNspDAO = new RemoteNspDAO();
		// String updateTime=remoteNspDAO.getLastUpdateTime();
		// if(updateTime.equals("")){
		// updateTime="";
		// }
		String sql = "Select  p.PARTNER_ID,p.PARTNER_CODE,p.PARTNER_KEY,p.NAME,p.ADDRESS,p.CONTACT_NAME,p.CONTACT_TEL,p.CONTACT_ADDRESS,p.CONTACT_NOTE,p.TECH_CONTACT_NAME,p.TECH_CONTACT_TEL,p.TECH_CONTACT_NOTE,p.NOTE,p.STATUS,p.CREATE_USER,p.CREATE_TIME,p.LAST_UPDATE_USER,p.LAST_UPDATE_TIME from OMC_PARTNER as p";
		QueryAssistantSQL assistantSQL = baseDao.newQueryAssistantSQL(sql);
		PageRender pageRender = new PageRender(0, 100000);
		List<Map> resultList = assistantSQL.queryByPage(
				baseDao.getConnection(), null, pageRender);
		if (resultList.size() > 0) {
			try {
				
				// 修改对应omc的同步人和同步时间
				//Long userId = loginUser.getUserId();

				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");// 设置日期格式
				String omcSql = "update OMC_PARTNER set LAST_SYNCHRONIZATION_TIME ='"
						+ df.format(new Date())
						+ "',LAST_SYNCHRONIZATION_USER= '" + userId + "'";
				QueryAssistantSQL updateParterSql = baseDao
						.newQueryAssistantSQL(omcSql);
				updateParterSql.updateSql(baseDao.getConnection());
				// 同步到nsp
				remoteNspDAO.saveNspPartner(resultList);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public void queryAbilityPartnerInterface(Long userId) throws SQLException {

		RemoteNspDAO remoteNspDAO = new RemoteNspDAO();
		QueryAssistantSQL assistantSQL = baseDao
				.newQueryAssistantSQL("Select PARTNER_INTERFACE_ID, PARTNER_ID, INTERFACE_ID, STATUS, CREATE_USER, CREATE_TIME,"
						+ "LAST_UPDATE_USER,LAST_UPDATE_TIME from OMC_PARTNER_INTERFACE where 1=1 order by PARTNER_INTERFACE_ID DESC");
		PageRender pageRender = new PageRender(0, 100000);
		List<Map> resultList = assistantSQL.queryByPage(
				baseDao.getConnection(), null, pageRender);
		// 同步到nsp

		try {
			if (resultList.size() > 0) {
				
				//Long userId = loginUser.getUserId();
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");// 设置日期格式
				QueryAssistantSQL updateParterInterface = baseDao
						.newQueryAssistantSQL("update OMC_PARTNER_INTERFACE set LAST_SYNCHRONIZATION_TIME= '"
								+ df.format(new Date())
								+ "',LAST_SYNCHRONIZATION_USER='"
								+ userId
								+ "' where 1=1");
				updateParterInterface.updateSql(baseDao.getConnection());
				remoteNspDAO.saveNspPartnerInterface(resultList);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// omc snytime snyuser

	}

	public void queryAbilityPartnerParamValue(Long userId) throws Exception {

		RemoteNspDAO remoteNspDAO = new RemoteNspDAO();

		String sql = "Select PARTNER_PARAM_VALUE_ID, PARTNER_INTERFACE_ID, INTERFACE_PARAM_ID, PARAM_VALUE_ID, PARAM_VALUE, NOTE,"
				+ "STATUS,CREATE_USER, CREATE_TIME,LAST_UPDATE_USER,LAST_UPDATE_TIME from OMC_PARTNER_PARAM_VALUE where 1=1";
		QueryAssistantSQL assistantSQL = baseDao.newQueryAssistantSQL(sql);

		PageRender pageRender = new PageRender(0, 100000);
		List<Map> resultList = assistantSQL.queryByPage(
				baseDao.getConnection(), null, pageRender);
		// 同步到nsp

		try {
			if (resultList.size() > 0) {
			
				//Long userId = loginUser.getUserId();
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");// 设置日期格式
				QueryAssistantSQL updateParterInterface = baseDao
						.newQueryAssistantSQL("update OMC_PARTNER_PARAM_VALUE set LAST_SYNCHRONIZATION_TIME= '"
								+ df.format(new Date())
								+ "',LAST_SYNCHRONIZATION_USER='"
								+ userId
								+ "' where 1=1");
				updateParterInterface.updateSql(baseDao.getConnection());
				remoteNspDAO.saveNspPartnerParamValue(resultList);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void queryAbilityInterfaceParam(Long userId) throws Exception {

		RemoteNspDAO remoteNspDAO = new RemoteNspDAO();
		QueryAssistantSQL assistantSQL = baseDao
				.newQueryAssistantSQL("Select INTERFACE_PARAM_ID, INTERFACE_ID, PARAM_KEY, PARAM_NAME, TYPE, NOTE,"
						+ "STATUS,CREATE_USER, CREATE_TIME,LAST_UPDATE_USER,LAST_UPDATE_TIME from OMC_INTERFACE_PARAM where 1=1");

		PageRender pageRender = new PageRender(0, 100000);
		List<Map> resultList = assistantSQL.queryByPage(
				baseDao.getConnection(), null, pageRender);
		// 同步到nsp

		try {
			if (resultList.size() > 0) {
				
				//Long userId = loginUser.getUserId();
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");// 设置日期格式
				QueryAssistantSQL updateParterInterface = baseDao
						.newQueryAssistantSQL("update OMC_INTERFACE_PARAM set LAST_SYNCHRONIZATION_TIME= '"
								+ df.format(new Date())
								+ "',LAST_SYNCHRONIZATION_USER='"
								+ userId
								+ "' where 1=1");
				updateParterInterface.updateSql(baseDao.getConnection());
				remoteNspDAO.saveNspInterfaceParam(resultList);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void queryAbilityInterface(Long userId) throws Exception {

		RemoteNspDAO remoteNspDAO = new RemoteNspDAO();

		// 更改表名称 QueryAssistantSQL 根据需要改
		QueryAssistantSQL assistantSQL = baseDao
				.newQueryAssistantSQL("Select INTERFACE_ID, INTERFACE_CODE, URL, NAME, TYPE, NOTE,"
						+ "STATUS,CREATE_USER, CREATE_TIME,LAST_UPDATE_USER,LAST_UPDATE_TIME from OMC_INTERFACE where 1=1");

		PageRender pageRender = new PageRender(0, 100000);
		List<Map> resultList = assistantSQL.queryByPage(
				baseDao.getConnection(), null, pageRender);
		// 同步到nsp

		try {
			if (resultList.size() > 0) {
				
				//Long userId = loginUser.getUserId();
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");// 设置日期格式
				QueryAssistantSQL updateParterInterface = baseDao
						.newQueryAssistantSQL("update OMC_INTERFACE set LAST_SYNCHRONIZATION_TIME= '"
								+ df.format(new Date())
								+ "',LAST_SYNCHRONIZATION_USER='"
								+ userId
								+ "' where 1=1");
				updateParterInterface.updateSql(baseDao.getConnection());
				remoteNspDAO.saveNspInterface(resultList);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void queryAbilityInterfaceParamValue(Long userId) throws Exception {

		RemoteNspDAO remoteNspDAO = new RemoteNspDAO();

		// 更改表名称 QueryAssistantSQL 根据需要改
		QueryAssistantSQL assistantSQL = baseDao
				.newQueryAssistantSQL("Select PARAM_VALUE_ID, INTERFACE_PARAM_ID, PARAM_VALUE_DIAPLAY, PARAM_VALUE, NOTE, STATUS,"
						+ "CREATE_USER,CREATE_TIME, LAST_UPDATE_USER,LAST_UPDATE_TIME from OMC_INTERFACE_PARAM_VALUE where 1=1");
		PageRender pageRender = new PageRender(0, 100000);
		List<Map> resultList = assistantSQL.queryByPage(
				baseDao.getConnection(), null, pageRender);
		// 同步到nsp

		try {
			if (resultList.size() > 0) {
				
				//Long userId = loginUser.getUserId();
				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");// 设置日期格式
				QueryAssistantSQL updateParterInterface = baseDao
						.newQueryAssistantSQL("update OMC_INTERFACE_PARAM_VALUE set LAST_SYNCHRONIZATION_TIME= '"
								+ df.format(new Date())
								+ "',LAST_SYNCHRONIZATION_USER='"
								+ userId
								+ "' where 1=1");
				updateParterInterface.updateSql(baseDao.getConnection());
				remoteNspDAO.saveNspInterfaceParamValue(resultList);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/*
	 * public void queryPartnerInterface() throws Exception{ RemoteNspDAO
	 * remoteNspDAO = new RemoteNspDAO(); String updateTime =
	 * remoteNspDAO.getLastUpdatePtInf(); QueryAssistantSQL assistantSQL =
	 * baseDao.newQueryAssistantSQL(
	 * "SELECT * FROM OMC_PARTNER_INTERFACE WHERE LAST_UPDATE_TIME >'"
	 * +updateTime+"'"); List<Map> resultList =
	 * assistantSQL.query(baseDao.getConnection(), null);
	 * 
	 * remoteNspDAO.saveNspPartnerInterface(resultList); }
	 */

	public void savePartnerInterface(PartnerInterface partnerInterface) {
		partnerInterfaceDao.save(partnerInterface);

		List partnerParamValueList = new ArrayList();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("SELECT a.INTERFACE_PARAM_ID  "
						+ "  FROM omc_interface_param a "
						+ " WHERE a.INTERFACE_ID =  "
						+ partnerInterface.getInterfaceId()
						+ "   and a.STATUS='1' ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		for (int i = 0; i < resultList.size(); i++) {
			Integer interfaceParamId = (Integer) resultList.get(i).get(
					"INTERFACE_PARAM_ID");

			PartnerParamValue partnerParamValue = new PartnerParamValue();
			partnerParamValue.setInterfaceParamId(interfaceParamId.longValue());
			partnerParamValue.setPartnerInterfaceId(partnerInterface
					.getPartnerInterfaceId());

			partnerParamValue.setCreateTime(new Date());
			partnerParamValue.setCreateUser(partnerInterface.getCreateUser());
			partnerParamValue.setLastUpdateTime(new Date());
			partnerParamValue.setLastUpdateUser(partnerInterface
					.getLastUpdateUser());
			partnerParamValue.setStatus("1");

			partnerParamValueList.add(partnerParamValue);

		}

		partnerParamValueDao.save(partnerParamValueList);

	}

	public void updatePartnerInterface(PartnerInterface partnerInterface) {
		partnerInterfaceDao.update(partnerInterface);
	}

	public void delete(List deleteList) {
		partnerInterfaceDao.remove(deleteList);
	}

	public void setPartnerParamValueDao(
			PartnerParamValueDao partnerParamValueDao) {
		this.partnerParamValueDao = partnerParamValueDao;
	}

}